package com.itheima.Service.ServiceImpl;

import com.itheima.Service.UserLoginService;
import com.itheima.mapper.UserLoginMapper;
import com.itheima.pojo.LoginDto;
import com.itheima.pojo.LoginInfo;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserLoginServiceImpl implements UserLoginService {


    @Autowired
    private UserLoginMapper userLoginMapper;

    /**
     * 用户端登录
     *
     * @param dto
     * @return
     */
    @Override
    public LoginInfo login(LoginDto dto) {
        User user = userLoginMapper.login(dto);
        if (ObjectUtils.isEmpty(user)) {
            return null;
        }
        Map<String, Object> claims = new HashMap<>();
        claims.put("id", user.getId());
        claims.put("username", user.getUsername());

        String token = JwtUtils.generateJwt(claims);

        return new LoginInfo(token);


    }
}
